package com.permissions.dispatcher.processor.impl;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;

/**
 * 系统弹框权限帮助类
 */
public class SystemAlertWindowHelper implements SensitivePermissionInterface {
    private ClassName permissionUtils = ClassName.get("com.permissions.dispatcher.library", "PermissionUtils");
    private ClassName settings = ClassName.get("ohos.sysappcomponents", "Settings");
    private ClassName intent = ClassName.get("ohos.aafwk.content", "Intent");
    private ClassName uri = ClassName.get("ohos.utils.net", "Uri");

    @Override
    public void addHasSelfPermissionsCondition(MethodSpec.Builder builder, String activityVar, String permissionField) {
        builder.beginControlFlow(
                "if ($T.hasSelfPermissions($N, $N) || $T.canDrawOverlays($N))",
                permissionUtils,
                activityVar,
                permissionField,
                settings,
                activityVar);
    }

    @Override
    public void addRequestPermissionsStatement(
            MethodSpec.Builder builder, String targetParam, String activityVar, String requestCodeField) {
        builder.addStatement(
                "$T intent = new $T($T.ACTION_MANAGE_OVERLAY_PERMISSION, $T.parse(\"package:\" + $N.getPackageName()))",
                intent,
                intent,
                settings,
                uri,
                activityVar);
        builder.addStatement("$N.startAbilityForResult(intent, $N)", targetParam, requestCodeField);
    }
}
